﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ArchivingDataModel;
using ArchivingDataModel.Enums;
using WebHelper.Helpers;
using System.Web.Security;

namespace ArchivingDataModel.InitializeData
{
    public class InitializeData
    {
        public static void Initialize()
        {
            object x="lock";
            lock(x)
            {
                IntializeCustomFieldsTypes();
                InializeCustomFields();
                IntializeImportSources();
                IntializeImportStatus();
                InitializeOrganizationsTypes();
                InitializeUser();
                
                System.Web.UI.Extended e=new System.Web.UI.Extended ();
            }
        }

        private static void InitializeOrganizationsTypes()
        {
            using (ArchivingDataEntities ent = new ArchivingDataEntities())
            {
                if (ent.TypeOfOrganizations.Count() == 0)
                {
                    string[] names =new string []{"ادارة","قسم"};
                    for (int i = 0; i < names.Length; i++)
                    {
                        string nameEn = names[i];
                        TypeOfOrganization item = new TypeOfOrganization
                        {
                            Type_Of_Organization_NameAR = nameEn,
                            Type_Of_Organization_NameEN = nameEn,
                        };
                        item.TypeOfOrganizations_Search = WebHelper.Helpers.StringHelper.ToUniqueStatement(item.Type_Of_Organization_NameAR + item.Type_Of_Organization_NameAR);
                        ent.AddToTypeOfOrganizations(item);
                    }
                }
                ent.SaveChanges();
            }
        }

        private static void InitializeUser()
        {
            using (ArchivingDataEntities ent = new ArchivingDataEntities())
            {
                var user = Membership.GetUser("admin");
                if (user == null)
                {
                    user = Membership.CreateUser("admin", "admin","a.b@c");
                }
                if (ent.Organizations.Count()==0)
                { 
                    ent.AddToOrganizations(new Organization()
                    {
                        Organization_Name_AR="المدير العام",
                        Organization_Name_EN = "المدير العام",
                        Organization_Number="01",
                        Is_External=0,
                        Is_Stand_Alone=1,
                        Organizations_Search = "المديرالعام",
                        TypeOfOrganization=ent.TypeOfOrganizations.FirstOrDefault()
                    });
                    ent.SaveChanges();
                }
                if (ent.Employees.Count() == 0)
                {
                    string userid=user.ProviderUserKey.ToString();
                    ent.AddToEmployees(new Employee()
                        {
                            Employee_NameAR="admin",
                            Employee_NameEN="admin",
                            Employees_Search="admin",
                            InnerPhone="",
                            Mobile="",
                            Organization=ent.Organizations.First(),
                            User_ID=userid
                        });
                    ent.SaveChanges();
                }
            }
        }


        protected static void IntializeCustomFieldsTypes()
        {
            using (ArchivingDataEntities ent = new ArchivingDataEntities())
            {
                if (ent.CusFieldsTypes.Count() == 0)
                {
                    string []names= Enum.GetNames(typeof(CustomFieldTypeEnum));
                    for (int i = 0; i < names.Length; i++)
                    {
                        long id = Convert.ToInt64(Enum.Parse(typeof(CustomFieldTypeEnum), names[i]));
                        string nameAr = "";
                        if (names[i] == CustomFieldTypeEnum.Boolean.ToString())
                        {
                            nameAr = "منطقي";
                        }
                        else if (names[i] == CustomFieldTypeEnum.ChooseFromList.ToString())
                        {
                            nameAr = "اخنيار من متعدد";
                        }
                        else if (names[i] == CustomFieldTypeEnum.Date.ToString())
                        {
                            nameAr = "تاريخ";
                        }
                        else if (names[i] == CustomFieldTypeEnum.MultiChoise.ToString())
                        {
                            nameAr = "اختيار قيم متعدده";
                        }
                        else if (names[i] == CustomFieldTypeEnum.Number.ToString())
                        {
                            nameAr = "رقم";
                        }
                        else if (names[i] == CustomFieldTypeEnum.Text.ToString())
                        {
                            nameAr = "نص";
                        }
                        string nameEn = names[i];
                        CusFieldsType item = new CusFieldsType
                        {
                            CFieldType_ID=id,
                            CFieldTypeName_Ar = nameAr,
                            CFieldTypeName_En = nameEn,
                            CFieldTypeNote_Ar = "",
                            CFieldTypeNote_En = ""
                        };
                        item.CFieldType__UniqueText=WebHelper.Helpers.StringHelper.ToUniqueStatement( item.CFieldTypeName_Ar+item.CFieldTypeName_En+item.CFieldTypeNote_Ar+item.CFieldTypeNote_En);
                        ent.AddToCusFieldsTypes(item);
                    }
                }
                ent.SaveChanges();
            }
        }
        protected static void InializeCustomFields()
        {
            using (ArchivingDataEntities ent = new ArchivingDataEntities())
            {
                if (ent.CusFields.Count() == 0)
                {
                    CusFieldsType[] types = ent.CusFieldsTypes.ToArray();
                    for (int i = 0; i < types.Length; i++)
                    {
                        CusFieldsType CusFieldsType=types[i];
                        for (int j = 0; j < 30; j++)
                        {
                            CusField item = new CusField();
                            item.CusFieldName = CusFieldsType.CFieldTypeName_En.Substring(0, 1) + (j + 1).ToString("D2");
                            item.CusFieldsType = CusFieldsType;
                            item.CusField_Note = "";
                            item.CusField__UniqueText = WebHelper.Helpers.StringHelper.ToUniqueStatement(item.CusFieldName  + item.CusField_Note);
                            ent.AddToCusFields(item);
                        }
                    }
                }
                ent.SaveChanges();
            }
        }
        protected static void IntializeImportSources()
        {
            using (ArchivingDataEntities ent = new ArchivingDataEntities())
            {
                if (ent.ImportSources.Count() == 0)
                {
                    string[] names = Enum.GetNames(typeof(ImportSourcesEnum));
                    for (int i = 0; i < names.Length; i++)
                    {
                        long id = Convert.ToInt64(Enum.Parse(typeof(ImportSourcesEnum), names[i]));
                        string nameAr = "";
                        if (names[i] == ImportSourcesEnum.ECS.ToString())
                        {
                            nameAr = "الاتصالات الادارية";
                        }
                        else if (names[i] == ImportSourcesEnum.LocalNetWork.ToString())
                        {
                            nameAr = "الشبكة الداخلية";
                        }
                        else if (names[i] == ImportSourcesEnum.FTP.ToString())
                        {
                            nameAr = "بروتوكول نقل الملفات";
                        }
                        else if (names[i] == ImportSourcesEnum.LocalPC.ToString())
                        {
                            nameAr = "الجهاز الحالي";
                        }
                        else if (names[i] == ImportSourcesEnum.Scanner.ToString())
                        {
                            nameAr = "الماسح الضوئي";
                        }
                        string nameEn = names[i];
                        ImportSource item = new ImportSource
                        {
                            ISource_ID = id,
                            ISourceName_Ar = nameAr,
                            ISourceName_En = nameEn,
                            ISourceNote_Ar = "",
                            ISourceNote_En = "",
                        };
                        item.ISource_UniqueText = WebHelper.Helpers.StringHelper.ToUniqueStatement(item.ISourceName_Ar + item.ISourceName_En + item.ISourceNote_Ar + item.ISourceName_En);
                        ent.AddToImportSources(item);
                    }
                }
                ent.SaveChanges();
            }
        }
        protected static void IntializeImportStatus()
        {
            using (ArchivingDataEntities ent = new ArchivingDataEntities())
            {
                if (ent.ImportStatus.Count() == 0)
                {
                    string[] names = Enum.GetNames(typeof(ImportStatusEnum));
                    for (int i = 0; i < names.Length; i++)
                    {
                        long id = Convert.ToInt64(Enum.Parse(typeof(ImportStatusEnum), names[i]));
                        string nameAr = "";
                        if (names[i] == ImportStatusEnum.Initialized.ToString())
                        {
                            nameAr = "البدايه";
                        }
                        else if (names[i] == ImportStatusEnum.Canceled.ToString())
                        {
                            nameAr = "تم الغاؤه";
                        }
                        else if (names[i] == ImportStatusEnum.Processing.ToString())
                        {
                            nameAr = "يتم معالجته";
                        }
                        else if (names[i] == ImportStatusEnum.Finished.ToString())
                        {
                            nameAr = "انتهى";
                        }
                        else if (names[i] == ImportStatusEnum.SourceRemoved.ToString())
                        {
                            nameAr = "تم حذفه";
                        }
                        else if (names[i] == ImportStatusEnum.ConnectionProblem.ToString())
                        {
                            nameAr = "بيانات اتصال خاطئه";
                        }
                        string nameEn = names[i];
                        ImportStatu item = new ImportStatu
                        {
                            IStatus_ID = id,
                            IStatusName_Ar = nameAr,
                            IStatusName_En = nameEn,
                            IStatusNote_Ar = "",
                            IStatusNote_En = "",
                        };
                        item.IStatus_UniqueText = WebHelper.Helpers.StringHelper.ToUniqueStatement(item.IStatusName_Ar + item.IStatusName_En + item.IStatusNote_Ar + item.IStatusName_En);
                        ent.AddToImportStatus(item);
                    }
                }
                ent.SaveChanges();
            }
        }

    }
}
